package com.winning.bi.etl;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class TaskFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TaskFrame frame = new TaskFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws ConfigurationException
	 */
	public TaskFrame() throws ConfigurationException {
		super("任务数据库设置");
		PropertiesConfiguration config = new PropertiesConfiguration("config/db.properties");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new FormLayout(
				new ColumnSpec[] { ColumnSpec.decode("40px"), ColumnSpec.decode("80px"),
						FormSpecs.UNRELATED_GAP_COLSPEC, ColumnSpec.decode("160px"), ColumnSpec.decode("90px") },
				new RowSpec[] { RowSpec.decode("21px"), RowSpec.decode("21px"), FormSpecs.LINE_GAP_ROWSPEC,
						RowSpec.decode("21px"), FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("21px"),
						FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("21px"), FormSpecs.LINE_GAP_ROWSPEC }));
		JPanel jPanel2 = new JPanel();

		JButton button4 = new JButton("确定");

		JButton button3 = new JButton("取消");

		contentPane.add(new JLabel("数据库URL:"), "2, 2, left, center");
		JTextField urlField = new JTextField();
		urlField.setText(config.getString("task.url"));

		contentPane.add(urlField, "4, 2, fill, top");
		contentPane.add(new JLabel("用户名:"), "2, 4, left, center");
		JTextField usernameField = new JTextField();
		System.out.println(config.getString("task.username"));
		usernameField.setText(config.getString("task.username"));
		contentPane.add(usernameField, "4, 4, fill, top");
		contentPane.add(new JLabel("密码"), "2, 6, left, center");
		JTextField password = new JTextField();
		password.setText(config.getString("task.password"));
		contentPane.add(password, "4, 6, fill, top");
		JPanel jPanel = new JPanel();
		setContentPane(jPanel);
		jPanel.add(contentPane, BorderLayout.NORTH);
		jPanel.add(jPanel2, BorderLayout.SOUTH);

		button4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				if (StringUtils.isNotBlank(urlField.getText()) && StringUtils.isNotBlank(usernameField.getText())
						&& StringUtils.isNotBlank(password.getText()))

				{
					JOptionPane.showMessageDialog(contentPane, "修改数据源后需重启该应用", "提示", JOptionPane.PLAIN_MESSAGE);
					config.clearProperty("task.url");
					config.clearProperty("task.username");
					config.clearProperty("task.password");
					config.setProperty("task.url", urlField.getText());
					config.setProperty("task.username", usernameField.getText());
					config.setProperty("task.password", password.getText());
					try {
						config.save();

					} catch (ConfigurationException e1) {

						e1.printStackTrace();
					}
					dispose();
				} else {
					if (StringUtils.isBlank(urlField.getText())) {
						JOptionPane.showMessageDialog(contentPane, "没有填写URL", "提示", JOptionPane.WARNING_MESSAGE);
						return;
					}
					if (StringUtils.isBlank(usernameField.getText())) {
						JOptionPane.showMessageDialog(contentPane, "没有填写用户名", "提示", JOptionPane.WARNING_MESSAGE);
						return;
					}

					if (StringUtils.isBlank(password.getText())) {
						JOptionPane.showMessageDialog(contentPane, "没有填写密码", "提示", JOptionPane.WARNING_MESSAGE);
						return;

					}

				}

			}
		});

		button3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
		});

		jPanel2.add(button3);
		jPanel2.add(button4);

	}

}
